<?php
namespace Home\Controller;
use Think\Controller;
class TemplateController extends InitController {

	//广告列表
	public function set_word_list(){

		//异步
		$page         = I('page',0,'intval');
		$limit        = '';
		$index        = 10;
		$page_all     = 0;

		//获取页数
		if(!empty($page)){
			$now_page = ($page + 1);
			$page_all = $page * $index;
			$limit    = $page_all.','.$index;
		}else{
			$limit    = '0,10';
		}

		$adList = D('Ad')->select_ad_list($this->uid,$limit);

		//用于ajax分页
		 if(IS_AJAX){

			if(!empty($adList)){
				$status = 1;
			}else{
				$status = -1;
			}
			return $this->ajaxReturn(array('data'=>$adList,'status'=>$status));die;
		}

		$this->assign(array(
			'adlist'=>$adList,
			'type_list' => array(
				'0' => '文字',
				'1' => '图片',
				'2' => '视频',
				'3' => '音频',
				'4' => '电话',
				'5' => '电话与链接',
			)
		));
		$this->display('set_word_list');
	}

	//设置文字广告
	public function set_word(){
		$id = I('tid',0,'intval');
		if(!empty($id)){
			$ad = D('Ad')->select_ad($this->uid,$id);
			$this->assign(array(
				'ad'=>$ad,
				'user_info'=>$this->info
			));
		}

		$this->display('set_word');
	}

	//设置图片广告
	public function set_picture(){
		$id = I('tid',0,'intval');
		$status = judge_member_probation($this->opt,$this->info);
		if(!empty($id)){
			$ad = D('Ad')->select_ad($this->uid,$id);
			$this->assign(array(
				'ad'=>$ad
			));
		}

		$this->assign(array(
			'status' => $status,
			'user_info'=>$this->info,
		));
		$this->display('set_picture');
	}

	//素材库
	public function library(){
		//异步
		$page         = I('page',0,'intval');
		$limit        = '';
		$index        = 33;
		$page_all     = 0;

		//获取页数
		if(!empty($page)){
			$now_page = ($page + 1);
			$page_all = $page * $index;
			$limit    = $page_all.','.$index;
		}else{
			$limit    = '0,33';
		}

		//$adList = M('ImageMaterial')->where("id > 0")->limit($limit)->order("id desc")->select();
		$adList = M('Product')->where("id > 0")->limit($limit)->order("id desc")->select();

		//排列数据套版
		$category_list = array();

		$i = 1;

		foreach($adList as $k=>$v){

			$category_list[$i][] =  $v; 

			if(($k+1) % 4 ==0){				
				$i++;	
			}
		}

		//用于ajax分页
		 if(IS_AJAX){

			if(!empty($category_list)){
				$status = 1;
			}else{
				$status = -1;
			}
			return $this->ajaxReturn(array('data'=>$category_list,'status'=>$status));die;
		}

		$this->assign(array(
			'adList'=>$category_list,
			

		));
		$this->display('img_library');
	}


	//异步删除
	public function set_word_delete(){
		$id = I('id',0,'intval');
		if(!IS_AJAX){
			return false;
		}
		$judge = D('Ad')->delete_ad($this->uid,$id);
		if($judge){
			$this->ajaxReturn(array('status'=>'1','msg'=>'删除成功'));
		}else{
			$this->ajaxReturn(array('status'=>'-1','msg'=>'网络原因，删除失败'));
		}
	}


	//提交广告()
	public function ajax_word(){
		if(!IS_AJAX){
			return false;
		}

		$id    = I('id',0,'intval');
		$title = I('title','','strip_tags,htmlspecialchars,trim'); //标题
		$thumb = I('thumb'); //图片
		$url   = I('url','','strip_tags,trim');  //链接
		$type  = I('type','0','trim');           //类型：  0:文字，1：图片 2：视频链接
		$video_link = I('video_link',' ','trim');   //视频链接
		$bottom_type = I('button_type','0','intval');//按钮类型
		$bottom_position = I('button_position','0','intval');//按钮位置
		$phone = I('phone');//
		$img_id = I('library','0','intval');//按钮位置
		

		if(empty($title)){
			$this->ajaxReturn(array('status'=>'-1','msg'=>'标题不能为空'));
		}
		
		//如果电话不为空的话,解密电话号码，不然会过滤+
		if(!empty($phone)){
			$phone = base64_decode($phone);
		}
		
	
		//解密url ,拼装链接
		if(!empty($url)){
			$url = base64_decode($url);
			//拼装链接
			if(!empty($url) && strpos($url,'http') === false){
				$url = 'http://'.$url;
			}
			if(!isUrl($url)){
				$this->ajaxReturn(array('status'=>'-1','msg'=>'请输入正确的URL地址'));
			}
		}
		
		//请上传图片
		if(!$thumb){
			$this->ajaxReturn(array('status'=>'-1','msg'=>'请上传图片'));			
		}
		
		if($type == 4 || $type == 5){
			if(empty($thumb)){
				$this->ajaxReturn(array('status'=>'-1','msg'=>'请上传图片'));
			}
			if(empty($bottom_type)){
				$this->ajaxReturn(array('status'=>'-1','msg'=>'请选择悬浮按钮类型'));
			}
		}
		/* if($type == 1){
			if(empty($url)){
				$this->ajaxReturn(array('status'=>'-1','msg'=>'链接不能为空'));
			}
		}else if($type == 4 || $type == 5){
			if(empty($thumb)){
				$this->ajaxReturn(array('status'=>'-1','msg'=>'请上传图片'));
			}
			if(empty($bottom_type)){
				$this->ajaxReturn(array('status'=>'-1','msg'=>'请选择悬浮按钮类型'));
			}	
		} */
		

		//按钮位置
		if(empty($bottom_type)){
			$bottom_position = 0;
		}

		if(!empty($id)){
			if($type=='2'){
				$data = array(
					'title'=>$title,
					'video_url'=>$video_link,
					'uid'=>$this->uid,
					'type'=>2,
					'createtime'=>NOW_TIME
				);
			}else{
				//文字，图片链接
				$data = array(
					'title'=>$title,
					'thumb'=>$thumb,
					'url'=>$url,
					'uid'=>$this->uid,
					'button_type'=>$bottom_type,
					'video_url'=>$video_link,
					'button_position'=>$bottom_position
				);

			}

			if(!empty($type)){
				$data['type'] = $type;
			}

			if(!empty($phone)){
				$data['phone'] = $phone;	
			}

			//素材主键id
			$data['img_id'] = $img_id;


			$judge = D('Ad')->update_ad($id,$data,$this->uid);
			if($judge !== false){
				$this->ajaxReturn(array('status'=>'1','msg'=>'更新成功','url'=>U('Home/Template/set_word_list')));
			}else{
				$this->ajaxReturn(array('status'=>'-1','msg'=>'网络原因，更新失败'));
			}
		}else{
			//视频链接
			if($type=='2'){
				$data = array(
					'title'=>$title,
					'video_url'=>$video_link,
					'uid'=>$this->uid,
					'type'=>2,
					'createtime'=>NOW_TIME
				);
			}else{
				//文字，图片链接
				$data = array(
					'title'=>$title,
					'thumb'=>$thumb,
					'url'=>$url,
					'uid'=>$this->uid,
					'createtime'=>NOW_TIME,
					'button_type'=>$bottom_type,
					'video_url'=>$video_link,
					'button_position'=>$bottom_position
				);
			}

			//表明是图片的标识
			if(!empty($data['thumb'])){
				$data['type'] = 1;
			}

			if(!empty($type)){
				$data['type'] = $type;
			}

			if(!empty($phone)){
				$data['phone'] = $phone;	
			}

			//素材主键id
			$data['img_id'] = $img_id;

			//插入数据
			$judge = D('Ad')->insert_ad($data);
			if($judge){
				$this->ajaxReturn(array('status'=>'1','msg'=>'设置广告成功','url'=>U('Home/Template/set_word_list')));
			}else{
				$this->ajaxReturn(array('status'=>'-1','msg'=>'网络原因，更新失败'));
			}
		}
	}

	//获取单张广告图片信息
	public function ad_info_ajax(){
		if(!IS_AJAX){
			return false;
		}
		$tid    = I('id',0,'intval');
        //获取模板详细信息
		$result = D('Ad')->select_ad($this->uid,$tid);
		if(!$result){
			$this->ajaxReturn(array('status'=>'-1','msg'=>'网路原因，插入失败'));
		}else{
			$this->ajaxReturn(array('status'=>'1','msg'=>'成功','data'=>$result));
		}
	}

	//异步请求页面
	public function ad_list_ajax(){
		if(!IS_AJAX){
			return false;
		}

		$ad = M('Ad')->where('uid = %d',$this->uid)->order('id desc')->select();

		$user_fans = M('User')->where('id = %d',$this->uid)->field("valid_time,level")->find();

		$judge_member = judge_member_probation($this->opt,$this->info);

		//限制广告
		if($this->info['level'] == '0' && !$judge_member){
			$this->ajaxReturn(array('error'=>1,'msg'=>'非会员不能使用'));
	    }

		if(!empty($ad)){
			$this->ajaxReturn(array('data'=>$ad,'status'=>'1'));			
		}else{			
			$this->ajaxReturn(array('data'=>'没有广告','status'=>'0'));	
		}
	}

	//异步请求页面
	public function ajax_template(){
		if(!IS_AJAX){
			return false;
		}
		$type = I('type','0','trim');

		$ad = M('Ad')->where('(type = %d  || type = 0 || type = 4 || type = 5) and uid = %d and status = 1',$type,$this->uid)->order('id desc')->select();

		if(!empty($ad)){
			$this->ajaxReturn(array('data'=>$ad,'status'=>'1'));			
		}else{			
			$this->ajaxReturn(array('data'=>'寻找不到对应模板','status'=>'0'));	
		}
	}

    //音乐异步秋叶面
    public function ajax_music_template(){
	    if(!IS_AJAX){
			return false;
		}
	    $ad = M('Mp3')->order('id desc')->limit('0,15')->select();

	   	if(!empty($ad)){
			$this->ajaxReturn(array('data'=>$ad,'status'=>'1'));	
		}else{
			$this->ajaxReturn(array('data'=>'没有音乐素材','status'=>'0'));			
		}
    }

    //分页音乐异步
  //   public function ajax_music_page(){
		// if(!IS_AJAX){
		// 	return false;
		// }
		// $page         = I('page',0,'intval');
		// $limit        = '';
		// $index        = 15;
		// $page_all     = 0;
		// $files        = 'id,title,createtime';
		// //获取页数
		// //$now_page = ($page + 1);
		// $page_all = $page * $index;
		// $limit    = $page_all.','.$index;

  //       $ad = M('Mp3')->order('id desc')->limit($limit)->select();

  //       if($ad){
  //           foreach($ad as $k=>$v){
		// 		$ad[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
		//     }
  //          	$status = 1;
  //       }else{

  //           $status = -1;
  //       }

  //      	return $this->ajaxReturn(array('data'=>$ad,'status'=>$status));die;
  //   }



    //音乐加载
    public function music_list_ajax(){
	    if(!IS_AJAX){
			return false;
		}
	    $ad = M('Mp3')->where('id > 0')->order('id desc')->limit('0,15')->select();

	   	if(!empty($ad)){
			$this->ajaxReturn(array('data'=>$ad,'status'=>'1'));	
		}else{
			$this->ajaxReturn(array('data'=>'没有音乐素材','status'=>'0'));			
		}
    }
	
	//素材时评加载
	public function ajax_material_video(){
		if(!IS_AJAX){
			return false;
		}
		
		$type   = I('type',1,'intval'); //1:加载视频列表,2:分类搜寻,3:搜寻标题
		$title  = I('title'); //搜寻标题
		$cid = I('cid');//分类id
		
		if($type == '1'){
			//视频加载列表
			$result = D('UploadMaterial')->upload_material_all();
			
			if(!empty($result)){
				$this->ajaxReturn(array('data'=>$result,'status'=>'1'));	
			}else{
				$this->ajaxReturn(array('data'=>'没有视频素材','status'=>'0'));			
			}
			
		}else if($type == '3'){
			//搜索标题
			if(empty($title)){
				$this->ajaxReturn(array('data'=>'请输入正确的标题','status'=>'0'));	
			}
			
			$result = D('UploadMaterial')->upload_material_search($where =' and title like "%'.$title.'%"');
			
			if(!empty($result)){
				$this->ajaxReturn(array('data'=>$result,'status'=>'1'));	
			}else{
				$this->ajaxReturn(array('data'=>'没有视频素材','status'=>'0'));			
			}
		}else if($type == '2'){
			//分类加载
			if(empty($cid)){
				$this->ajaxReturn(array('data'=>'分类参数有误','status'=>'0'));	
			}
			
			$result = D('UploadMaterial')->upload_material_category($cid);
			if(!empty($result)){
				$this->ajaxReturn(array('data'=>$result,'status'=>'1'));	
			}else{
				$this->ajaxReturn(array('data'=>'没有视频素材','status'=>'0'));			
			}
			
		}else{
			
			$this->ajaxReturn(array('data'=>'参数有误','status'=>'0'));
			
		}

	}
	
	//产品分类
	public function ajax_prodcut_category(){
		if(!IS_AJAX){
			return false;
		}

		$pid = I('pid',0,'intval');

		$category_list = array();

		$category_list['category'] = M('ProductCategory')->where(array('parent_id'=>0,'status'=>1))->order("taxis desc")->select();

		if(!empty($pid)){
			$category_list['sub'] = M('ProductCategory')->where(array('parent_id'=>$pid,'status'=>1))->select();
		}else{
			$category_list['sub'] = M('ProductCategory')->where(array('parent_id'=>$category_list['category'][0]['id'],'status'=>1))->select();
		}
		
		if(!empty($category_list['category'])){
			$this->ajaxReturn(array('data'=>$category_list,'status'=>'1'));	
		}else{
			$this->ajaxReturn(array('data'=>'没有产品','status'=>'0'));			
		}
	}

	//产品列表
	public function ajax_product_list(){
		if(!IS_AJAX){
			return false;
		}

		$pid = I('pid',0,'intval');
		$sub_id = I('sub_id',0,'intval');

		$cid = array();

		if(empty($pid)){
			if(empty($sub_id)){
				$cid[] = $parent_id = M('ProductCategory')->where(array('parent_id'=>$pid,'status'=>1))->order("taxis desc")->getField('id');
			}
		}else{
			$cid[] = $parent_id = $pid;
		}

		if(empty($sub_id)){
			$ids = M('ProductCategory')->where(array('parent_id'=>$parent_id,'status'=>1))->order("taxis desc")->field('id')->select();

			foreach($ids as $v){
				$cid[] = $v['id'];
			}

		}else{
			$cid[] = M('ProductCategory')->where(array('id'=>$sub_id,'status'=>1))->order("taxis desc")->getField('id');
		}

		$where['cid'] = array('in',$cid);
		$where['status'] = '1';

		$product = M('Product')->where($where)->field("id,thumb,title")->limit("0,30")->select();


		if(!empty($product)){
			$this->ajaxReturn(array('data'=>$product,'status'=>'1'));	
		}else{
			$this->ajaxReturn(array('data'=>'没有产品','status'=>'0'));			
		}
	}

	//插入产品
	public function product_info_ajax(){
		if(!IS_AJAX){
			return false;
		}
		$tid    = I('id',0,'intval');

        //获取模板详细信息
		$result = M('Product')->where('id = %d',$tid)->find();

		if(!$result){
			$this->ajaxReturn(array('status'=>'-1','msg'=>'网路原因，插入失败'));
		}else{
			$this->ajaxReturn(array('status'=>'1','msg'=>'成功','data'=>$result));
		}
	}

	//搜索产品
	public function search_product(){
		if(!IS_AJAX){
			return false;
		}
		$name = I('name','','trim');

		if(!empty($name)){
			$where["title"] = array("like", "%".$name."%");
		}
		
		$product = M('Product')->where($where)->field("id,thumb,title")->limit("0,30")->select();

		if(!empty($product)){
			$this->ajaxReturn(array('data'=>$product,'status'=>'1'));	
		}else{
			$this->ajaxReturn(array('data'=>'','status'=>'0'));			
		}
	}
}
